import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/common/no_permissions_widget.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_date_type.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/generated/assets.dart';

import 'warning_disposal_controller.dart';

/// 报警预警
class WarningDisposalPage extends StatelessWidget {
  WarningDisposalPage({super.key});

  final controller = Get.put(WarningDisposalController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          if (!controller.isNotPermissions) {
            return const NoPermissionsWidget();
          }
          return Scaffold(
            backgroundColor: const Color(0xFFF7FBFE),
            appBar: CustomAppBar(
              titleText: '报警预警',
            ),
            body: Container(
              padding: EdgeInsets.only(left: 16.w),
              margin: EdgeInsets.only(top: 16.w),
              child: CustomEasyRefreshList<AlarmRespEntity>(
                dataLoad: controller.getData,
                key: controller.globalKey,
                itemBuilder: (AlarmRespEntity item, int index) {
                  return CustomTitleChartPage(
                    contentPadding: EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
                    title: item.companyName,
                    titleRightWidget: controller.getStatueView(item.status),
                    child: Stack(
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '点位:', value: item.deviceCode)),
                                  // '${item.alarmLevel.toChinese}级')
                                Expanded(child: _getTextView(title: '报警级别:', value: CustomDateType.getAlarmType(item.alarmLevel, item.dataType),),)
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '系统-类别:', value:  '${item.systemName}-${item.monitorName}',),),
                                Expanded(child: _getTextView(title: '报警数值:', value: '${item.val}')),
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '系统名称:', value: item.systemName)),
                                Expanded(child: _getTextView(title: '监测类型:', value: item.monitorName)),
                              ],
                            ),
                            _getTextView(title: '报警类型:', value: CustomDateType.getAlarmTypeByValue(item.dataType)),
                            _getTextView(title: '报警开始:', value: item.alarmStartTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(item.alarmStartTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                            _getTextView(title: '报警结束:',value: item.alarmEndTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(item.alarmEndTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):DateTime.now().toStr),
                            AppGaps.vGap6,
                          ],
                        ),
                        Visibility(
                          visible: item.status != 3,
                          child: Positioned(
                            right: 0,
                            bottom: 0,
                            child: SizedBox(
                              width: 56.w,
                              height: 36.w,
                              child: Stack(
                                alignment: Alignment.center,
                                children: [
                                  SvgPicture.asset(
                                    Assets.svgIconEnter,
                                    fit: BoxFit.cover,
                                  ),
                                  Icon(
                                    Icons.arrow_forward,
                                    size: 20.w,
                                    color: Colors.white,
                                  ).marginOnly(left: 7.w, top: 6.w)
                                ],
                              ),
                            ),
                          ),
                        )
                      ],
                    ).clickOnce(() {
                      controller.goDetails(item);
                    }),
                  );
                },
              ),
            ),
          );
        });
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }


}
